home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / language / abcst.arc / ABCINTRO.DOC < prev    next >
Text File  |  1990-02-01  |  8KB  |  276 lines

  1. A SHORT INTRODUCTION TO THE ABC LANGUAGE
  2.  
  3. While the full documentation about ABC is in the ABC Programmer's Handbook,
  4. this article should give you just enough information to get going.
  5.  
  6. THE LANGUAGE
  7. ABC is an imperative language originally designed as a replacement for
  8. BASIC: interactive, very easy to learn, but structured, high-level,
  9. and easy to use. ABC has been designed iteratively, and the present
  10. version is the 4th iteration. The previous versions were called B (not
  11. to be confused with the predecessor of C).
  12.  
  13. It is suitable for general everyday programming, the sort of
  14. programming that you would use BASIC, Pascal, or AWK for. It is not a
  15. systems-programming language. It is an excellent teaching language,
  16. and because it is interactive, excellent for prototyping. It is much
  17. faster than 'bc' for doing quick calculations.
  18.  
  19. ABC programs are typically very compact, around a quarter to a fifth
  20. the size of the equivalent Pascal or C program. However, this is not
  21. at the cost of readability, on the contrary in fact (see the examples
  22. below).
  23.  
  24. ABC is simple to learn due to the small number of types in the
  25. language (five). If you already know Pascal or something similar you
  26. can learn the whole language in an hour or so.  It is easy to use
  27. because the data-types are very high-level.
  28.  
  29. The five types are:
  30.    numbers: unbounded length, with exact arithmetic the rule
  31.    texts (strings): also unbounded length
  32.    compounds: records without field names
  33.    lists: sorted collections of any one type of items (bags or multi-sets)
  34.    tables: generalised arrays with any one type of keys, any one type
  35.        of items (finite mappings).
  36.  
  37. THE ENVIRONMENT
  38. The implementation includes a programming environment that makes
  39. producing programs very much easier, since it knows a lot about the
  40. language, and can therefore do much of the work for you. For instance,
  41. if you type a W, the system suggests a command completion for you:
  42.     W?RITE ?
  43.  
  44. If that is what you want, you press [tab], and carry on typing the
  45. expression; if you wanted WHILE, you type an H, and the system changes
  46. the suggestion to match:
  47.     WH?ILE ?:
  48.  
  49. This mechanism works for commands you define yourself too. Similarly,
  50. if you type an open bracket or quote, you get the closing bracket or
  51. quote for free. You can ignore the suggestions if you want, and just
  52. type the commands full out.
  53.  
  54. There is support for workspaces for developing different programs.
  55. Within each workspace variables are persistent, so that if you stop
  56. using ABC and come back later, your variables are still there as you
  57. left them. This obviates the need for file-handling facilities: there
  58. is no conceptual difference between a variable and a file in ABC.
  59.  
  60. The language is strongly-typed, but without declarations. Types are
  61. determined from context.
  62.  
  63. IMPLEMENTATIONS
  64. The sources for the Unix version are being posted to
  65. comp.sources.unix; the binaries to comp.binaries.{mac, ibm.pc,
  66. atari.st}. They should also be available from some servers (for
  67. instance by anonymous ftp from hp4nl.nluug.nl [number] and mcsun.eu.net
  68. [number]).
  69.  
  70. There is an irregular newsletter available from us, and a book "The
  71. ABC Programmer's Handbook" by L. Geurts, L. Meertens and S. Pemberton,
  72. will be published by Prentice-Hall this year (ISBN 0-13-000027-2).
  73.  
  74.  
  75. ADDRESS
  76.     ABC Implementations
  77.     CWI/AA
  78.     Kruislaan 413
  79.     1098 SJ AMSTERDAM
  80.     The Netherlands
  81.  
  82.     Email: abc@cwi.nl
  83.  
  84. EXAMPLES
  85. The (second) best way to appreciate the power of ABC is to see some
  86. examples (the first is to use it). In what follows, >>> is the
  87. prompt from ABC:
  88.  
  89. NUMBERS
  90.     >>> WRITE 2**1000
  91.     107150860718626732094842504906000181056140481170553360744375038837
  92.     035105112493612249319837881569585812759467291755314682518714528569
  93.     231404359845775746985748039345677748242309854210746050623711418779
  94.     541821530464749835819412673987675591655439460770629145711964776865
  95.     42167660429831652624386837205668069376
  96.  
  97.     >>> PUT 1/(2**1000) IN x
  98.     >>> WRITE 1 + 1/x
  99.     107150860718626732094842504906000181056140481170553360744375038837
  100.     035105112493612249319837881569585812759467291755314682518714528569
  101.     231404359845775746985748039345677748242309854210746050623711418779
  102.     541821530464749835819412673987675591655439460770629145711964776865
  103.     42167660429831652624386837205668069377
  104.  
  105. TEXTS
  106.     >>> PUT ("ha " ^^ 3) ^ ("ho " ^^ 3) IN laugh
  107.     >>> WRITE laugh
  108.     ha ha ha ho ho ho 
  109.  
  110.     >>> WRITE #laugh
  111.     18
  112.  
  113.     >>> PUT "Hello! "^^1000 IN greeting
  114.     >>> WRITE #greeting
  115.     7000
  116.  
  117. LISTS
  118.     >>> WRITE {1..10}
  119.     {1; 2; 3; 4; 5; 6; 7; 8; 9; 10}
  120.     >>> PUT {1..10} IN l
  121.     >>> REMOVE 5 FROM l
  122.     >>> INSERT pi IN l
  123.     >>> WRITE l
  124.     {1; 2; 3; 3.141592653589793; 4; 6; 7; 8; 9; 10}
  125.  
  126.     >>> PUT {} IN ll
  127.     >>> FOR i IN {1..3}:
  128.             INSERT {1..i} IN ll
  129.     >>> WRITE ll
  130.     {{1}; {1; 2}; {1; 2; 3}}
  131.     >>> FOR l IN ll:
  132.             WRITE l /
  133.     {1}
  134.     {1; 2}
  135.     {1; 2; 3}
  136.     >>> WRITE #ll
  137.     3
  138.  
  139. COMPOUNDS
  140.     >>> PUT ("Square root of 2", root 2) IN c
  141.     >>> WRITE c
  142.     ("Square root of 2", 1.414213562373095)
  143.     >>> PUT c IN name, value
  144.     >>> WRITE name
  145.     Square root of 2
  146.     >>> WRITE value
  147.     1.414213562373095
  148.  
  149. A TELEPHONE LIST
  150. This uses the table data-type. In use, tables resemble arrays:
  151.  
  152.     >>> PUT {} IN tel
  153.     >>> PUT 4054 IN tel["Jennifer"]
  154.     >>> PUT 4098 IN tel["Timo"]
  155.     >>> PUT 4134 IN tel["Guido"]
  156.  
  157.     >>> WRITE tel["Jennifer"]
  158.     4054
  159.  
  160. You can write all ABC values out. Tables are kept sorted on the keys:
  161.     >>> WRITE tel
  162.     {["Guido"]: 4134; ["Jennifer"]: 4054; ["Timo"]: 4098}
  163.  
  164. The keys function returns a list:
  165.     >>> WRITE keys tel
  166.     {"Guido"; "Jennifer"; "Timo"}
  167.  
  168.     >>> FOR name IN keys tel:
  169.            WRITE name, ":", tel[name] /
  170.     Guido: 4134
  171.     Jennifer: 4054
  172.     Timo: 4098
  173.  
  174. You can define your own commands:
  175.  
  176.     HOW TO DISPLAY t:
  177.        FOR name IN keys tel:
  178.           WRITE name<<10, tel[name] /
  179.  
  180.     >>> DISPLAY tel
  181.     Guido      4134
  182.     Jennifer   4054
  183.     Timo       4098
  184.  
  185. To find the user of a given number, you can use a quantifier:
  186.     >>> IF SOME name IN keys tel HAS tel[name] = 4054:
  187.            WRITE name
  188.     Jennifer
  189.  
  190. Or create the inverse table:
  191.     >>> PUT {} IN subscriber
  192.     >>> FOR name IN keys tel:
  193.            PUT name IN subscriber[tel[name]]
  194.  
  195.     >>> WRITE subscriber[4054]
  196.     Jennifer
  197.  
  198.     >>> WRITE subscriber
  199.     {[4054]: "Jennifer"; [4098]: "Timo"; [4134]: "Guido"}
  200.  
  201. Commands and functions are polymorphic:
  202.     >>> DISPLAY subscriber
  203.     4054       Jennifer
  204.     4098       Timo
  205.     4134       Guido
  206.  
  207. Functions may return any type. Note that indentation is significant -
  208. there are no BEGIN-END's or { }'s:
  209.  
  210.     HOW TO RETURN inverse t:
  211.        PUT {} IN inv
  212.        FOR k IN keys t:
  213.           PUT k IN inv[t[k]]
  214.        RETURN inv
  215.  
  216.     >>> WRITE inverse tel
  217.     {[4054]: "Jennifer"; [4098]: "Timo"; [4134]: "Guido"}
  218.  
  219.     >>> DISPLAY inverse inverse tel
  220.     Guido      4134
  221.     Jennifer   4054
  222.     Timo       4098
  223.  
  224. A CROSS-REFERENCE INDEXER
  225.  
  226. 'Text files' are represented as tables of numbers to strings:
  227.  
  228.     >>> DISPLAY poem
  229.     1         I've never seen a purple cow
  230.     2         I hope I never see one
  231.     3         But I can tell you anyhow
  232.     4         I'd rather see than be one
  233.  
  234. The following function takes such a document, and returns the
  235. cross-reference index of the document: a table from words to lists of
  236. line-numbers:
  237.  
  238.     HOW TO RETURN index doc:
  239.        PUT {} IN where
  240.        FOR line.no IN keys doc:
  241.           TREAT LINE
  242.        RETURN where
  243.     TREAT LINE:
  244.        FOR word IN split doc[line.no]:
  245.           IF word not.in keys where:
  246.          PUT {} IN where[word]
  247.           INSERT line.no IN where[word]
  248.  
  249. TREAT LINE here is a refinement, directly supporting
  250. stepwise-refinement. 'split' is a function that splits a string into
  251. its space-separated words:
  252.  
  253.     >>> WRITE split "Hello world"
  254.     {[1]: "Hello"; [2]: "world"}
  255.  
  256.     >>> DISPLAY index poem
  257.     But        {3}
  258.     I          {2; 2; 3}
  259.     I'd        {4}
  260.     I've       {1}
  261.     a          {1}
  262.     anyhow     {3}
  263.     be         {4}
  264.     can        {3}
  265.     cow        {1}
  266.     hope       {2}
  267.     never      {1; 2}
  268.     one        {2; 4}
  269.     purple     {1}
  270.     rather     {4}
  271.     see        {2; 4}
  272.     seen       {1}
  273.     tell       {3}
  274.     than       {4}
  275.     you        {3}
  276.